//
//  ContactListener.mm
//  PhysicsBox2d
//
//  Created by quipu on 11/04/08.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ContactListener.h"
#import "Ball.h"

void ContactListener::BeginContact(b2Contact* contact) {
	b2Body* bodyA = contact->GetFixtureA()->GetBody();
	b2Body* bodyB = contact->GetFixtureB()->GetBody();
	Ball* bodyNodeA = (Ball*)bodyA->GetUserData();
	Ball* bodyNodeB = (Ball*)bodyB->GetUserData();
    
	// start plunger on contact
	if ([bodyNodeA isKindOfClass:[Ball class]] && [bodyNodeB isKindOfClass:[Ball class]]) {
//        bodyNodeA.sprite.color = ccYELLOW;
//        bodyNodeB.sprite.color = ccYELLOW;
        NSLog(@"a tag: %d b tag: %d static: %d", bodyNodeA.tag, bodyNodeB.tag, _currentBallTag);
        if (bodyA->IsActive() && bodyNodeA.tag == _currentBallTag && ((bodyNodeA.tag > 7 && bodyNodeB.tag <= 7) || (bodyNodeA.tag <= 7 && bodyNodeB.tag > 7))) {
            NSLog(@"va cham vat A va vat B");
            [bodyNodeB setHeart:bodyNodeB.heart - bodyNodeA.damage];
            if (bodyNodeB.heart <= 0) {
                
//                for (b2Fixture* f = bodyB->GetFixtureList(); f; f = f->GetNext()) {
//                    bodyB->DestroyFixture(f);
//                }
                CCParticleFire* explosion = [[CCParticleFire alloc] init];
                [explosion setPosition:bodyNodeB.sprite.position];
                [[bodyNodeB parent] addChild:explosion];
                [explosion runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.8], [CCCallBlock actionWithBlock:^{
                    [explosion removeFromParentAndCleanup:YES];
                }], nil]];
                [explosion release];
                
                [bodyNodeB removeFromParentAndCleanup:YES];
                bodyB->SetLinearVelocity(b2Vec2(0, 0));
                bodyB->SetAngularVelocity(0);
            }
        }
        else if (bodyB->IsActive() && bodyNodeB.tag == _currentBallTag && ((bodyNodeA.tag > 7 && bodyNodeB.tag <= 7) || (bodyNodeA.tag <= 7 && bodyNodeB.tag > 7))) {
            NSLog(@"va cham vat B va vat A");
            [bodyNodeA setHeart:bodyNodeA.heart - bodyNodeB.damage];
            if (bodyNodeA.heart <= 0) {
               //                for (b2Fixture* f = bodyA->GetFixtureList(); f; f = f->GetNext()) {
//                    bodyA->DestroyFixture(f);
//                }
                CCParticleFire* explosion = [[CCParticleFire alloc] init];
                [explosion setPosition:bodyNodeA.sprite.position];
                [[bodyNodeA parent] addChild:explosion];
                [explosion runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.8], [CCCallBlock actionWithBlock:^{
                    [explosion removeFromParentAndCleanup:YES];
                }], nil]];
                [explosion release];
                
                [bodyNodeA removeFromParentAndCleanup:YES];
                bodyA->SetLinearVelocity(b2Vec2(0, 0));
                bodyA->SetAngularVelocity(0);

            }
        }
	}
}

void ContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
}

void ContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
}

void ContactListener::EndContact(b2Contact* contact) {
    b2Body* bodyA = contact->GetFixtureA()->GetBody();
	b2Body* bodyB = contact->GetFixtureB()->GetBody();
	BodyNode* bodyNodeA = (BodyNode*)bodyA->GetUserData();
	BodyNode* bodyNodeB = (BodyNode*)bodyB->GetUserData();
    
	// start plunger on contact
	if ([bodyNodeA isKindOfClass:[Ball class]] && [bodyNodeB isKindOfClass:[Ball class]]) {
        [(Ball*)bodyNodeA resetColor]; 
        [(Ball*)bodyNodeB resetColor]; 
	}
}
